<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>返工单</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="${pageContext.request.contextPath }/assets/css/layui.css"  media="all">
	<link rel="stylesheet" href="${pageContext.request.contextPath }/assets/css/toastr.min.css"  media="all">
	<link rel="stylesheet" href="${pageContext.request.contextPath}/assets/ztree/css/zTreeStyle/zTreeStyle.css">
	
	<script type="text/javascript" src="${pageContext.request.contextPath }/assets/js/jquery.min.js" charset="utf-8"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/assets/ztree/js/jquery.ztree.all.min.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/assets/ztree/js/jquery.ztree.core.js"></script>  
    <script type="text/javascript" src="${pageContext.request.contextPath}/assets/ztree/js/jquery.ztree.excheck.js"></script>  
    <script type="text/javascript" src="${pageContext.request.contextPath}/assets/ztree/js/jquery.ztree.exedit.js"></script>
	<style type="text/css">
	</style>
</head>
<body>
<div class="demoTable" style="padding:10px 0 0 15px">
    <div class="layui-inline">
       	<input class="layui-input" id="keyword" placeholder="请输入工序"/>
    </div>
    <button class="layui-btn layui-btn-blue" style="margin: 0 0 0 10px" data-type="reload"><i class="layui-icon layui-icon-search">  搜索</i></button>
</div>
<!-- 上表 -->
<script type="text/html" id="toolbarDemo">
	<div class="layui-btn-container">
		<button class="layui-btn layui-btn-blue" lay-event="add"><i class="layui-icon layui-icon-add-1">  新增</i></button>
		<button class="layui-btn layui-btn-blue" lay-event="delete"><i class="layui-icon layui-icon-delete">  批量删除</i></button>
	</div>
</script>
<h2 style="text-align: center;color:#666">返工单</h2>
<table class="layui-hide" id="rework" lay-filter="rework"></table>
<script type="text/html" id="barDemo">
	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="${pageContext.request.contextPath }/assets/layui.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath }/assets/layui.all.js"></script>
<script src="${pageContext.request.contextPath }/assets/js/toastr.min.js" charset="utf-8"></script>
<script type="text/javascript">
//日期格式转换
function createTime(v){
	var date = new Date(v);
    var y = date.getFullYear();
    var M = date.getMonth()+1;
    M = M<10?'0'+M:M;
    var d = date.getDate();
    d = d<10?("0"+d):d;
    var h = date.getHours();
    h = h<10?("0"+h):h;
    var m = date.getMinutes();
    m = m<10?("0"+m):m;
    var str = y+"-"+M+"-"+d+" "+h+":"+m;
    return str;
}
//日期格式转换
function date2String(timestamp){
	var d = new Date(timestamp);
	var date = (d.getFullYear()) + "-" + 
       (d.getMonth() + 1<10?"0"+(d.getMonth() + 1):d.getMonth() + 1) + "-" +
       (d.getDate()<10?"0"+d.getDate():d.getDate()) + " " + 
       (d.getHours()<10?"0"+d.getHours():d.getHours()) + ":" + 
       (d.getMinutes()<10?"0"+d.getMinutes():d.getMinutes()) + ":" + 
       (d.getSeconds()<10?"0"+d.getSeconds():d.getSeconds());
       
       return date;
}
layui.use(['table','layer','upload','form','laydate'], function(){
	var table = layui.table;
	var layer = layui.layer,
    form = layui.form,
	laydate = layui.laydate;
	var $ = layui.jquery, active = {
			reload:function () {
				var keyword = $("#keyword").val();
				table.reload('contenttable',{
					method:'get',
					where:{keyword:keyword}
					,page: {
						curr: 1 //重新从第 1 页开始
					}
				});
			}
		}
	$('.layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
	table.render({
	    elem: '#rework'
	    ,url:'${pageContext.request.contextPath }/rework/selectReworkList.do'
	    ,toolbar: '#toolbarDemo'
	    ,title: '返工单'
	    ,id :'contenttable'
	    ,limits:[10,20,30]
	    ,cols: [[
		  {type: 'checkbox', fixed: 'left',width:50}
	      ,{field:'', title:'序号', sort: true, type:'numbers', width: 60}
	      ,{field:'fgNumber', title:'返工单号'}
	      ,{field:'fgName', title:'组件名称'}
	      ,{field:'fgCode', title:'组件编号'}
	      ,{field:'fgDisposeDate', title:'返工日期',templet:function(row){
	    	  return date2String(row.fgDisposeDate);
	      }}
	      ,{field:'dProcess', title:'工序'}
	      ,{field:'fgWay', title:'处理方法'}
	      ,{field:'fgDisponer', title:'处理人'}
	      ,{field:'fgReworkDate', title:'合格日期',templet:function(row){
	    	  if(row.fgReworkDate==""){
	    		  return "";
	    	  }else{
		    	  return date2String(row.fgReworkDate);
	    	  }
	      }}
	      ,{fixed: 'right', title:'操作', toolbar: '#barDemo',width:150}
	    ]]
	    ,page: true
	});
  
	//头工具栏事件
	table.on('toolbar(rework)', function(obj){
		var checkStatus = table.checkStatus(obj.config.id);
		var content = 	'<form class="layui-form layui-card-body" id="addFormId">'+
						'<input class="layui-hide" id="dId" name="dId">'+
						'<div class="layui-form-item"><label class="layui-form-label">质检单号</label>'+
							'<input readonly class="layui-input layui-input-inline" lay-verify="ddNumber" id="ddNumber" type="text" value="" placeholder="点击选择待返工单" style="width:300px;">'+
							'<div class="layui-inline" style="margin-top: 12px;">'+
								'<font style="color:red; font-size: 24px;">*</font>'+
							'</div>'+
						'</div>'+
						'<div class="layui-form-item"><label class="layui-form-label">组件名称</label><input readonly class="layui-input" lay-verify="fgName" id="fgName" name="fgName" type="text" value="" style="width:300px;"></div>'+
						'<div class="layui-form-item"><label class="layui-form-label">组件编号</label><input readonly class="layui-input" lay-verify="fgCode" id="fgCode" name="fgCode" type="text" value="" style="width:300px;"></div>'+
						'<div class="layui-form-item"><label class="layui-form-label">返工日期</label><input readonly class="layui-input" lay-verify="fgDisposeDate" id="fgDisposeDate" name="fgDisposeDate" type="text" value="" style="width:300px;"></div>'+
						'<div class="layui-form-item">'+
							'<label class="layui-form-label">工序</label>'+
							'<div class="layui-input-block" style="width:300px;">'+
								'<select class="layui-select" lay-verify="dProcess" id="dProcess" name="dProcess">'+
									'<option value="膜丝">膜丝</option>'+
									'<option value="组装">组装</option>'+
									'<option value="离心">离心</option>'+
									'<option value="固化">固化</option>'+
									'<option value="切割">切割</option>'+
									'<option value="检漏">检漏</option>'+
									'<option value="评价">组件评价</option>'+
									'<option value="气密">气密检测</option>'+
								'</select>'+
							'</div>'+
						'</div>'+
						'<div class="layui-form-item"><label class="layui-form-label">问题描述及处理方法</label><textarea class="layui-textarea" lay-verify="fgWay" id="fgWay" name="fgWay" type="text" value="" style="width:300px;"></textarea></div>'+
						'<div class="layui-form-item"><label class="layui-form-label">合格日期</label><input class="layui-input" lay-verify="fgReworkDate" id="fgReworkDate" name="fgReworkDate" type="text" value="" style="width:300px;"></div>'+
					  		'<div class="layui-form-item"><div class="layui-input-block">'+
								'<button id="subBtn1" class="layui-btn layui-btn-blue" lay-submit lay-filter="addform">提交</button><button type="reset" id="addReset" class="layui-btn layui-btn-primary" style="margin-left:140px">重置</button>'+
							'</div></div>'+
						'</form>';
	    switch(obj.event){
	    	case 'add':
	    		layer.open({
					  type: 1 		//Page层类型
					  ,area: ['500px', '600px'] //宽  高
					  ,title: '新增'
					  ,shade: 0.1 	//遮罩透明度
					  ,maxmin: true //允许全屏最小化
					  ,anim: 1 		//0-6的动画形式，-1不开启
					  ,content: content
					  ,success:function(layero, index){
						  form.render();
							//日期
							laydate.render({
							   elem: '#fgReworkDate'
							   ,type: 'datetime'
							});
						$("#ddNumber").click(function(){
							layer.open({
  						        type: 1 					//Page层类型
  						        ,area: ['95%', '600px']   //宽  高
  						        ,title: '待返工质检单列表'
  						        ,shade: 0.6 				//遮罩透明度
  						        ,maxmin: true			    //允许全屏最小化
  						        ,anim: 1 					//0-6的动画形式，-1不开启
  						        /* ,content: $("#lookupTable2") */
  						        ,content: '<table class="layui-hide" id="stayRework" lay-filter="stayRework"></table>'
  						        ,success:function(){
  						            layui.use(['table'], function(){
  						                var table = layui.table;
  						                table.render({
  						                    elem: '#stayRework'
  						                    ,url:'${pageContext.request.contextPath }/rework/selectStayReworkList.do'
  						                    ,title: '待返工质检单列表'
  						                    ,limits:[10,20,30]
  						                    ,cols: [[
  						                        {field:'', title:'序号', sort: true, type:'numbers', width: 60}
  						                        ,{field:'ddNumber', title:'质检单号'}
  						                        ,{field:'ddName', title:'名称'}
  						                        ,{field:'remark01', title:'条码'}
  						                        ,{field:'remark02', title:'工位'}
  						                        ,{field:'testPerson', title:'检验人'}
  						                        ,{field:'testDate', title:'检验日期',templet:function(row){
  						                        	return date2String(row.testDate);
  						                        }}
  						                        ,{field:'remark05', title:'返工日期',templet:function(row){
  						                        	return date2String(row.remark05);
  						                        }}
  						                    ]]
  						                    ,page: true
  						                });
  						            });
  						        }
  							})
						})
						table.on('row(stayRework)', function(obj){
  				        			$("#dId").val(obj.data.dId);
  				        			$("#ddNumber").val(obj.data.ddNumber);
  				        			$("#fgName").val(obj.data.ddName);
  				        			$("#fgCode").val(obj.data.remark01);
  				        			$("#fgDisposeDate").val(obj.data.remark05);
  				        			layer.close(layer.index);
									
		  							/* $.ajax({
		  								type:'get',
		  				        		url:'${pageContext.request.contextPath }/rework/selectDisposeRejectsDetailsById.do',
		  				        		data:{keyword:dId},
		  				        		dataType:'json',
		  				        		success: function(res){
		  				        			$("#dId").val(dId);
		  				        			$("#dNumber").val(dNumber);
		  				        			$("#fgName").val(res.ddName);
		  				        			$("#fgCode").val(res.remark01);
		  				        			$("#fgDisposeDate").val(date2String(res.remark05));
		  				        			layer.close(layer.index);
		  				        		}
		  						}) */
							})
					  }
				});
	      	break;
	    	case 'delete':
	        	var data = checkStatus.data;
	        	var idArr = new Array();
	        	if(data.length==0){
	        		toastr.warning("请至少选择一条记录！");
	        	}
	        	var idArr = new Array();
	        	for(var i=0;i<data.length;i++){
	        		idArr[i] = data[i].fgId;
	    		    layer.confirm('确定删除吗？', function(index){
	    			    $.ajax({
	    			    	type:'post',
	    			    	url:'${pageContext.request.contextPath }/rework/delReworkArr.do',
	    			    	data:{"idArr":idArr},
	    			    	success:function(num){
	    			    		if(num>0){
	    			    			toastr.success("删除了"+num+"条数据！");
	    			    			setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
	    								//关闭模态框
	    								// 父页面刷新
	    								window.location.reload();  
	    							},2000);
	    			    		}else{
	    			    			toastr.warning("删除失败！");
	    			    		}
	    			    	}
	    			    		
	    			    })
	    			    layer.close(index);
	    			  });
	        	}
	      	break;
	    };
	});
  
	//监听行工具事件
	table.on('tool(rework)', function(obj){
		var data = obj.data;
		if(obj.event === 'del'){
		  layer.confirm('确定删除吗？', function(index){
			var idArr = new Array();
		    var fgId = data.fgId;
		    idArr[0] = fgId;
		    $.ajax({
		    	type:'post',
		    	url:'${pageContext.request.contextPath }/rework/delReworkArr.do',
		    	data:{"idArr":idArr},
		    	success:function(data){
		    		if(data>0){
		    			toastr.success("删除成功！");
		    			setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
							//关闭模态框
							// 父页面刷新
							window.location.reload();  
						},2000);
		    		}else{
		    			toastr.warning("删除失败！");
		    		}
		    	}
		    		
		    })
		    layer.close(index);
		  });
		} else if(obj.event === 'edit'){
			var content1 = '<form class="layui-form layui-card-body" id="editFormId">'+
								'<input class="layui-hide" id="dId" name="dId">'+
								'<input class="layui-hide" id="fgId" name="fgId">'+
								'<div class="layui-form-item"><label class="layui-form-label">处置单号</label><input readonly class="layui-input" id="dNumber" type="text" value="" placeholder="点击选择待返工单" style="width:300px;"></div>'+
								'<div class="layui-form-item"><label class="layui-form-label">组件名称</label><input readonly class="layui-input" lay-verify="fgName" id="fgName" name="fgName" type="text" value="" style="width:300px;"></div>'+
								'<div class="layui-form-item"><label class="layui-form-label">组件编号</label><input readonly class="layui-input" lay-verify="fgCode" id="fgCode" name="fgCode" type="text" value="" style="width:300px;"></div>'+
								'<div class="layui-form-item"><label class="layui-form-label">返工日期</label><input readonly class="layui-input" lay-verify="fgDisposeDate" id="fgDisposeDate" name="fgDisposeDate" type="text" value="" style="width:300px;"></div>'+
								'<div class="layui-form-item"><label class="layui-form-label">工序</label><input class="layui-input" lay-verify="dProcess" id="dProcess" name="dProcess" type="text" value="" style="width:300px;"></div>'+
								'<div class="layui-form-item"><label class="layui-form-label">问题描述及处理方法</label><textarea class="layui-textarea" lay-verify="fgWay" id="fgWay" name="fgWay" type="text" value="" style="width:300px;"></textarea></div>'+
								'<div class="layui-form-item"><label class="layui-form-label">合格日期</label><input class="layui-input" lay-verify="fgReworkDate" id="fgReworkDate" name="fgReworkDate" type="text" value="" style="width:300px;"></div>'+
								'<div class="layui-form-item"><div class="layui-input-block">'+
									'<button class="layui-btn layui-btn-blue" lay-submit lay-filter="editform">提交</button><button type="reset" id="editReset" class="layui-btn layui-btn-primary" style="margin-left:140px">重置</button>'+
								'</div></div>'+
							'</form>';
			layer.open({
				  type: 1 		//Page层类型
				  ,area: ['500px', '450px'] //宽  高
				  ,title: '修改'
				  ,shade: 0.1 	//遮罩透明度
				  ,maxmin: true //允许全屏最小化
				  ,anim: 1 		//0-6的动画形式，-1不开启
				  ,content: content1
				  ,success:function(layero, index){
					  form.render();
				  }
			});
		}
	});
	/**
     * 表单校验
     */
    form.verify({
    	//value：表单的值、item：表单的DOM对象
    	ddNumber: function(value, item){
    		if(value == ''){
    			return '质检单号不能为空';
    		}
    	}
    })
    /**
     * 通用表单提交(AJAX方式)（新增）
     */
    form.on('submit(addform)', function (data) {
    	$("#subBtn1").attr("disabled",true);
    	var fgCode = $("#fgCode").val();
    	$.ajax({
			type : "post",
			url : '${pageContext.request.contextPath}/rework/insertRework.do',
			data : $("#addFormId").serialize(),
			cache : false,
			async:false
			}).done(
				function(res) {
					if (res == 1) {
						toastr.success('新增成功！');
					}
					setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
						//关闭模态框
						// 父页面刷新
						window.location.reload();  
					},2000);
				}
			).fail(
				function(res) {
					toastr.error('新增失败！');
				}
			)
			return false;
    });
    /**
     * 通用表单提交(AJAX方式)(修改)
     */
    form.on('submit(editform)', function (data) {
    	$.ajax({
			url : '${pageContext.request.contextPath}/disposeRejects/updateDisposeRejects.do',
			data : $("#editFormId").serialize(),
			cache : false,
			async:false
			/* 
				success: function (data) {},
				error: function (XMLHttpRequest, textStatus, errorThrown) {},
				beforeSend: function () {},
				complete: function () {} 
			 */
			}).done(
				function(res) {
					if (res == 1) {
						toastr.success('修改成功！');
					}
					setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
						//关闭模态框
						// 父页面刷新
						window.location.reload();  
					},2000);
				}
			).fail(
				function(res) {
					toastr.error('修改失败！');
				}
			)
			return false;
    });
});
toastr.options.positionClass = 'toast-top-center'; //提示框的位置设置为中上
</script>
</body>
</html>